package com.cqwu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cqwu.bean.Person;
import com.cqwu.bean.Sysuser;
import com.cqwu.biz.SysUserService;
import com.cqwu.util.BaseAction;
import com.cqwu.util.PageBean;

/**
* @Title: TestAction
* @Description: 
* @Company: 物联网俱乐部
* @author: lj
* @date: Nov 17, 2015 3:49:22 PM
*/
@SuppressWarnings("serial")
@Namespace("/test")
@ParentPackage("struts-default")
@Controller
@Scope("prototype")
public class TestAction extends BaseAction{
	@Autowired
	@Qualifier("sysUserService")
	private SysUserService sysUserService;
	private String pageNum ;
	private String userName;
	
	@Action(value = "getUser", results = { @Result(name="success",location = "/index.jsp"),@Result(name="input",location = "/index.jsp")})
	public String getUser(){
		Sysuser sys = sysUserService.get(1);
		System.out.println(sys.getRealName());
		getRequest().setAttribute("person", sys);
		//测试分页
		System.out.println("address = "+ getRequest().getRequestURL());
		System.out.println(getRequest().getRequestURL()+"");
		PageBean<Sysuser> page =  sysUserService.getPageData(pageNum,getRequest().getRequestURL()+"");
		
		List<Sysuser> list = page.getList();
		System.out.println("pagesize = "+list.size());
		getRequest().setAttribute("page", page);
		return SUCCESS;
	}
	
	@Action(value="TestJSON",results={})
	public void testJSON(){
		System.out.println("username = "+ userName +"test json....");
		List<Person> list = new ArrayList<Person>();
		list.add(new Person("likang", 21, "男"));
		list.add(new Person("lisi",10, "女"));
		list.add(new Person("haha", 54, "男"));
		JSONArray jsa = JSONArray.fromObject(list);
		try {
			System.out.println(jsa.toString());
			getResponse().setCharacterEncoding("UTF-8");
			PrintWriter pr = getResponse().getWriter();
			
			pr.print(jsa.toString());
			//pr.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Action(value="Interceptor",results={@Result(name="succeed",location="/index.jsp")})
	public String Intecepter(){
		System.out.println("test intecpter...");
		return "succeed";
	}
	
	public String testIN(){
		System.out.println("添加了额struts2的配置文件..");
		return null;
	}
	
	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
}
